//
//  PropertyRentDetailViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/17.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "PropertyRentDetailViewController.h"

#define IMAGEHEIGHT (168 + kStatusAndNavBarH)

@interface PropertyRentDetailViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (strong, nonatomic) UIImageView * img;

@end

@implementation PropertyRentDetailViewController

static NSString * cellID = @"cell";
static NSString * PropertyRentDetailInfoTableViewCellID = @"PropertyRentDetailInfoTableViewCell";
static NSString * PropertyRentDetailContentTableViewCellID = @"PropertyRentDetailContentTableViewCell";
static NSString * PorpertyRentDetailImgTableViewCellID = @"PorpertyRentDetailImgTableViewCell";
static NSString * PropertyRentMoneyTableViewCellID = @"PropertyRentMoneyTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableview];
    [self registCell];
    self.tableview.contentInset = UIEdgeInsetsMake(IMAGEHEIGHT - kStatusAndNavBarH, 0, 0, 0);
    [self wr_setNavBarBackgroundAlpha:0];
    [self wr_setNavBarShadowImageHidden:YES];
    [self wr_setNavBarBarTintColor:[UIColor whiteColor]];
    [self.tableview setScrollsToTop:NO];
    [self.tableview addSubview:self.img];
//    [self RequestData];
    WeakSelf;
    [self.img sd_setImageWithURL:[NSURL URLWithString:self.model.bigpic] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        weakSelf.img.image = image;
    }];
    [self.tableview reloadData];
}

//#pragma mark - 招租明细
//-(void)RequestData
//{
//    NSString * url = [NSString stringWithFormat:@"/rest/gbPropertyletController/getPropertylet?id=%@",self.model.RentID];
//    [[HTTPClient shareClient] requestWithPath:url AndReturnBlock:^(NSDictionary *dic) {
//
//    } AndFailMessage:^(NSString *message, NSString *errorCode) {
//
//    }];
//}

-(void)registCell
{
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableview registerClass:[PropertyRentDetailInfoTableViewCell class] forCellReuseIdentifier:PropertyRentDetailInfoTableViewCellID];
    [self.tableview registerClass:[PropertyRentDetailContentTableViewCell class] forCellReuseIdentifier:PropertyRentDetailContentTableViewCellID];
    [self.tableview registerClass:[PorpertyRentDetailImgTableViewCell class] forCellReuseIdentifier:PorpertyRentDetailImgTableViewCellID];
    [self.tableview registerClass:[PropertyRentMoneyTableViewCell class] forCellReuseIdentifier:PropertyRentMoneyTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - kHomeLineH) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return [UIView new];
    }
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    bgView.backgroundColor = SFM242;
    return bgView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return self.model.titleHeight + 40 + self.model.detailHeight;
    }else if(indexPath.section == 1)
    {
        return 65;
    }else
    {
        if (indexPath.row == 0) {
            
            return self.model.contentHeight + 63;
        }
        return 209;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if(section == 1)
    {
        return 1;
    }else
    {
        return 1 + 1;
    }
}

- (PropertyRentDetailInfoTableViewCell *)extracted:(UITableView * _Nonnull)tableView {
    PropertyRentDetailInfoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PropertyRentDetailInfoTableViewCellID];
    return cell;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        PropertyRentDetailInfoTableViewCell * cell = [self extracted:tableView];
        [cell sendRentModel:self.model];
        return cell;
    }else if(indexPath.section == 1)
    {
        PropertyRentMoneyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PropertyRentMoneyTableViewCellID];
        [cell sendRentModel:self.model ];
        return cell;
    }else
    {
        if (indexPath.row == 0) {
            PropertyRentDetailContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PropertyRentDetailContentTableViewCellID];
            [cell sendRentModel:self.model];
            return cell;
        }else
        {
            PorpertyRentDetailImgTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PorpertyRentDetailImgTableViewCellID];
            [cell.img sd_setImageWithURL:[NSURL URLWithString:self.model.bigpic] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                cell.img.image = image;
            }];
            return cell;
        }
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(UIImageView *)img
{
    if (_img == nil) {
        _img=[[UIImageView alloc] initWithFrame:CGRectMake(0, -IMAGEHEIGHT , WIDTH, IMAGEHEIGHT)];
        _img.contentMode = UIViewContentModeScaleAspectFill;
        _img.clipsToBounds = YES;
    }
    return _img;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY > -IMAGEHEIGHT)
    {
        CGFloat alpha = (offsetY + IMAGEHEIGHT) / kStatusAndNavBarH;
        [self wr_setNavBarBackgroundAlpha:alpha];
        if (alpha > 0.5) {
            [self wr_setNavBarTintColor:[UIColor colorWithRed:0 green:0.478431 blue:1 alpha:1.0]];
            [self wr_setStatusBarStyle:UIStatusBarStyleDefault];
            [self wr_setNavBarTitleColor:SFM51];
        } else {
            [self wr_setNavBarTintColor:[UIColor whiteColor]];
            [self wr_setNavBarTitleColor:[UIColor whiteColor]];
            [self wr_setStatusBarStyle:UIStatusBarStyleLightContent];
        }
    }
    else
    {
        [self wr_setNavBarBackgroundAlpha:0];
        [self wr_setNavBarTintColor:[UIColor whiteColor]];
        [self wr_setNavBarTitleColor:[UIColor whiteColor]];
        [self wr_setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    
    //限制下拉的距离
    if(offsetY < -400) {
        [scrollView setContentOffset:CGPointMake(0, -400)];
    }
    // 改变图片框的大小 (上滑的时候不改变)
    // 这里不能使用offsetY，因为当（offsetY < LIMIT_OFFSET_Y）的时候，y = LIMIT_OFFSET_Y 不等于 offsetY
    CGFloat newOffsetY = scrollView.contentOffset.y;
    if (newOffsetY < -IMAGEHEIGHT)
    {
        self.img.frame = CGRectMake(0, newOffsetY, WIDTH, -newOffsetY);
    }
}

- (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(CGSizeMake(newSize.width*2, newSize.height*2));
    [image drawInRect:CGRectMake (0, 0, newSize.width*2, newSize.height*2)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
